<?php

function file_get_contents_post($url, $post)
{
    $options = [
        'http' => [
            'method'  => 'POST',
            // 'content' => 'name=caiknife&email=caiknife@gmail.com',
            'content' => http_build_query($post),
        ],
    ];

    $result = file_get_contents($url, false, stream_context_create($options));

    return $result;
}


function curl_post($url, $post)
{
    $options = [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $post,
    ];

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

/*
 * 借阅-购买-前台传的数据字符串转数组
 * id,num-id,num,....
 * 以-分隔条目，以,分隔内容
 * */
function bookinfo_strtoarr($liststr){
    $lista=explode(",",$liststr);
    $list=array();
    foreach($lista as $k=>$v){
        $tmp=explode("-",$lista[$k]);
        //数量为空或0不加入待生成信息
        if($tmp[1]=="" || $tmp[1]==0){
            continue;
        }
        array_push($list,array("book_id"=>$tmp[0],"num"=>$tmp[1]));
    }
    return $list;
}

/*
 * 检测敏感词汇，返回去敏感词汇后
 * */
function sensitivecheck($str){
    $url='http://www.hoapi.com/index.php/Home/Api/check';
    $options = [
        'http' => [
            'method'  => 'POST',
            // 'content' => 'name=caiknife&email=caiknife@gmail.com',
            'content' => http_build_query(["str"=>$str]),
        ],
    ];
    
    $result = file_get_contents($url, false, stream_context_create($options));
    $d=json_decode($result,JSON_UNESCAPED_UNICODE);
    if($d["status"]){
        return $str;
    }else{
        return $d["data"]["new_str"];
    }
}
//ajax输出
function echoJson($j){
    echo json_encode($j,JSON_UNESCAPED_UNICODE);
}
//系统参数设置
function getSysSetting($c){
    $d=M('Setting')->select();
    foreach($d as $k=>$v){
        switch($v['name']){
            case 'rentmax_md':$c['rentmax_md']=$v['value'];break;
            case 'rentmax_wx':$c['rentmax_wx']=$v['value'];break;
            case 'act_freenum_name':$c['act_freenum_name']=$v['value'];break;
            case 'rent_freenum_name':$c['rent_freenum_name']=$v['value'];break;
            default:break;
        }
    }
    return $c;
}

// 调试使用
function dd($value){
    if (is_array($value)) {
        echo json_encode($value);
        exit();
    }
    var_dump($value);
    exit();
}

function config($value) {
    $value = M('setting')->where(['name' => $value])->getField('value');
    if (! $value) {
        throw new Exception('key not exist');
    }
    return $value;
}